/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.PhieuGD_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class PhieuGD_DAO {
    
     public static String Get_MaGD() throws SQLException{
        String MaGD = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "GD");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaGD = Rs.getString(1);
        }
        return MaGD;
    }
    
    public static ArrayList<PhieuGD_DTO> GetALL()
    {
        ArrayList<PhieuGD_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from PhieuGD" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                PhieuGD_DTO Phieu = new PhieuGD_DTO();
                Phieu.setMaGD(rs.getString("MaGD"));
                Phieu.setMaNV(rs.getString("MaNV"));
                Phieu.setMaKH(rs.getString("MaKH"));
                Phieu.setMaCT(rs.getString("MaCT"));
                Phieu.setNgayLap(rs.getDate("NgayLap"));
                Phieu.setLoaiGD(rs.getInt("LoaiGD"));
                Phieu.setTinhTrangPhieu(rs.getInt("TinhTrangPhieu"));
                ds.add(Phieu);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(PhieuGD_DTO Phieu)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayLap = fmt.format(Phieu.getNgayLap());
            String sql = "Insert PhieuGD Values('"+Phieu.getMaGD()+"','"+Phieu.getMaNV()+"','"+Phieu.getMaKH()+"','"+Phieu.getMaCT()+"','"+NgayLap+"','"+Phieu.getLoaiGD()+"','"+Phieu.getTinhTrangPhieu()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(PhieuGD_DTO Phieu)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayLap = fmt.format(Phieu.getNgayLap());
            String sql = "Update PhieuGD Set MaNV = '"+ Phieu.getMaNV() +"', MaKH = '"+Phieu.getMaKH()+"', MaCT = '"+Phieu.getMaCT()+"', NgayLap = '"+ NgayLap+ "',LoaiGD = "+Phieu.getLoaiGD()+",TinhTrangPhieu ="+Phieu.getTinhTrangPhieu()+" where MaGD = '"+ Phieu.getMaGD()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maGD)
    {
        boolean flag = false;
        try {
            String sql = "Delete from PhieuGD where MaDT ='"+maGD+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
